﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Core.Interfaces;
using Core.Interfaces.IDAO;
using Core.Unity;
using Microsoft.Practices.Unity;

namespace Core.Reports
{
    public partial class ThirdReportViewer : Form
    {
        private IDistrictDAO _districtDAO;
        private List<IDistrict> _districts;
        private List<ICustomer> _customers;
        private ICustomerDAO _customerDAO;
        private IOwnedBuildingDAO _ownedBuildingDAO;


        public ThirdReportViewer()
        {
            InitializeComponent();
            _districtDAO = ResolverContainer.Resolver.Resolve<IDistrictDAO>();
            _customerDAO = ResolverContainer.Resolver.Resolve<ICustomerDAO>();
            _ownedBuildingDAO = ResolverContainer.Resolver.Resolve<IOwnedBuildingDAO>();
            _customers = _customerDAO.GetAll(false);
            _districts = _districtDAO.GetAll(false);
            iDistrictBindingSource.DataSource = _districts;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            iCustomerBindingSource.Clear();
            var ob = _ownedBuildingDAO.GetAll(true);
            var bb =ob.Where(x => x.Building.District.DistrictId == ((IDistrict)iDistrictBindingSource.Current).DistrictId).ToList();
            var cust = _customers.Where(x => bb.Any(y => y.Customer.CustomerID == x.CustomerID));
            iCustomerBindingSource.DataSource = cust;
            int EndPrice = 0;
            foreach (var ownedBuilding in bb)
            {
                if (cust.Any(x=>cust.Any(y=>y.CustomerID==ownedBuilding.Customer.CustomerID)))
                {
                    EndPrice += ownedBuilding.EndPrice;
                }
            }
            label2.Text = "Общая стоимость: " + EndPrice+" р.";
            iDistrictBindingSource.ResetBindings(false);
        }   
    }
}
